//koa项目的入口文件
const Koa = require('koa')
const router = require('./router')
const cors = require('koa2-cors')
const bodyParser = require('koa-bodyparser')

const app = new Koa()

// 加载路由中间件
// 调用router.allowedMethods()获得一个中间件，当发送了不符合的请求时，会返回 `405 Method Not Allowed` 或 `501 Not Implemented`
app.use(cors())
app.use(router.allowedMethods())
app.use(bodyParser())
app.use(router.routes())
// app.use(async (ctx, next) => {
//     ctx.set("Access-Control-Allow-Origin", "*")
//     ctx.set("Access-Control-Allow-Headers", "X-Requested-With")
//     ctx.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
//     ctx.set("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS")
//     const start = new Date()
//     await next()
//     const ms = new Date() - start
//     console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
//   })

app.listen(3000,()=>{
    console.log('Server is running at http://localhost:3000');
})